package arpui;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.Separator;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableView.TableViewSelectionModel;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import arpui.bean.Device;
import arpui.common.FXStatusBar;

public class ConfigMulti extends Application{
	private FXStatusBar statusBar = null;
	
	private TableView tvAll = null;
	private TableView tvSel = null;
	private Button selectAllBtn = null;
	private Button selectOneBtn = null;
	private Button removeAllBtn = null;
	private Button removeOneBtn = null;
	
	private TableView<Device> logTV = null;
	
	private Button startBtn = null;
	private TextField usernameFld = null;
	private PasswordField passwordFld = null;
	private TextField ips = null;
	private TextField ipe = null;
	private TextField ipmask = null;
	private TextField ipgw = null;
	
	
	public ConfigMulti(Stage parentStage){
		Stage stage = new Stage();
		stage.initOwner(parentStage);
		initUI(stage);
		addAction();
		stage.show();
	}
	
	public ConfigMulti(){}
	
	@Override
	public void start(Stage arg0) throws Exception {
		initUI(arg0);
		addAction();
		arg0.show();
	}
	
	public void initUI(Stage stage){
		BorderPane root = new BorderPane();
		
		VBox selectDevPane = getSelectDevPane();
		HBox centerPane = getCenterPane();
		
		root.setTop(selectDevPane);
		root.setCenter(centerPane);
		
		statusBar = new FXStatusBar();
		root.setBottom(statusBar);
		statusBar.setTipInfo("AAABBB...", 0);
		
		Scene scene = new Scene(root,680,600);
		stage.setScene(scene);
	}

	private VBox getSelectDevPane() {
		VBox mainBox = new VBox();
		
		HBox mainCon = new HBox();
		mainBox.setPadding(new Insets(5));
		mainBox.setSpacing(10);
		mainCon.setSpacing(10);
		
		tvAll = getTableView();
		tvSel = getTableView();

		VBox btnBox = new VBox();
		selectAllBtn = new Button("全选");
		selectAllBtn.setPrefWidth(100);
		removeAllBtn = new Button("取消全选");
		removeAllBtn.setPrefWidth(100);
		
		selectOneBtn = new Button(">");
		selectOneBtn.setPrefWidth(100);
		removeOneBtn = new Button("<");
		removeOneBtn.setPrefWidth(100);
		btnBox.setSpacing(5);
		btnBox.getChildren().addAll(new Label(),new Label(),new Label(),
				selectAllBtn,removeAllBtn,selectOneBtn,removeOneBtn);
		
		mainCon.getChildren().add(tvAll);
		mainCon.getChildren().add(btnBox);
		mainCon.getChildren().add(tvSel);
		
		Label tmpInfo = new Label("请选择需要配置的设备");
		mainBox.getChildren().add(tmpInfo);
		mainBox.getChildren().add(mainCon);
		mainBox.getChildren().add(new Separator());
		
		return mainBox;
	}
	
	public TableView getTableView(){
		TableView	tmp = new TableView();
		TableColumn noCol = new TableColumn("No");
		noCol.setMaxWidth(60);
		noCol.setCellValueFactory(new PropertyValueFactory<Device, String>("no"));
		
		TableColumn macCol = new TableColumn("MacAddress");
		macCol.setMinWidth(120);
		macCol.setCellValueFactory(new PropertyValueFactory<Device, String>("macAddress"));
		
		TableColumn devCol = new TableColumn("DeviceName");
		devCol.setMinWidth(100);
		devCol.setCellValueFactory(new PropertyValueFactory<Device, String>("deviceName"));
		
		tmp.getColumns().addAll(noCol,macCol,devCol);
		tmp.setPrefSize(300, 270);
		
		
		tmp.getItems().add(d);
		
		return tmp;
	}
	
	Device d = new Device((1) + "", "192.168.1.1" ,
			"255.255.255.0", "192.168.1.254",
			"FF FF FF FF FF FF" ,
			"Switch" );
	
	private HBox getCenterPane(){
		HBox mainBox = new HBox();
		mainBox.setPadding(new Insets(10));
		mainBox.setSpacing(8);
		
		Pane formPane = new Pane();
		formPane.setPrefWidth(390);
		
		
		logTV = new TableView();
		logTV.setPrefWidth(290);
		TableColumn noCol = new TableColumn("No");
		noCol.setMaxWidth(60);
		noCol.setCellValueFactory(new PropertyValueFactory<Device, String>("no"));
		
		TableColumn macCol = new TableColumn("MacAddress");
		macCol.setMinWidth(120);
		macCol.setCellValueFactory(new PropertyValueFactory<Device, String>("macAddress"));
		
		TableColumn retCol = new TableColumn("Result");
		retCol.setCellValueFactory(new PropertyValueFactory<Device, String>("retStr"));
		logTV.getColumns().addAll(noCol,macCol,retCol);
		
		ObservableList items = logTV.getItems();
		d.setRetStr("配置成功");
		items.add(d);
		
		VBox logBox = new VBox();
		logBox.setSpacing(10);
		logBox.getChildren().addAll(new Label("配置结果"),logTV);
		
		mainBox.getChildren().add(formPane);
		mainBox.getChildren().add(new Separator(Orientation.VERTICAL));
		mainBox.getChildren().add(logBox);
		return mainBox;
	}

	public void addAction(){
		selectOneBtn.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent arg0) {
				TableViewSelectionModel selectionModel = tvAll.getSelectionModel();
				int selectedIndex = selectionModel.getSelectedIndex();
				if(selectedIndex == -1){
					System.out.println("您没有选择一行!");
					return ;
				}
				Object selectedItem = selectionModel.getSelectedItem();
				
				System.out.println(selectedItem+(selectedIndex+""));

				ObservableList items = tvAll.getItems();
				items.remove(selectedIndex);
				tvSel.getItems().add(selectedItem);
			}
		});
	}
	
	public static void main(String[] args) {
		launch(null);
	}
}
